What is @metamask/object-multiplex?
@metamask/object-multiplex is a utility for creating and managing multiple independent streams of data over a single connection. It is particularly useful in scenarios where you need to multiplex several communication channels over a single transport layer, such as in browser extensions or inter-process communication.
What are @metamask/object-multiplex's main functionalities?
Creating a Multiplex Stream
This feature allows you to create a new multiplex stream instance. This instance can be used to manage multiple independent streams over a single connection.
const ObjectMultiplex = require('@metamask/object-multiplex');
const stream = new ObjectMultiplex();
Creating Substreams
This feature allows you to create substreams within the main multiplex stream. Each substream can be used independently to send and receive data.
const ObjectMultiplex = require('@metamask/object-multiplex');
const stream = new ObjectMultiplex();
const substream1 = stream.createStream('substream1');
const substream2 = stream.createStream('substream2');
Sending Data Over Substreams
This feature allows you to send data over a specific substream. The data sent over one substream is isolated from other substreams.
const ObjectMultiplex = require('@metamask/object-multiplex');
const stream = new ObjectMultiplex();
const substream = stream.createStream('substream');
substream.write('Hello, World!');
Receiving Data From Substreams
This feature allows you to receive data from a specific substream. You can set up event listeners to handle incoming data on each substream.
const ObjectMultiplex = require('@metamask/object-multiplex');
const stream = new ObjectMultiplex();
const substream = stream.createStream('substream');
substream.on('data', (data) => {
console.log('Received data:', data);
});
Other packages similar to @metamask/object-multiplex
multiplex
The 'multiplex' package provides similar functionality for creating multiple independent streams over a single connection. It is a more general-purpose library compared to @metamask/object-multiplex, which is tailored for use in browser extensions.
mux-demux
The 'mux-demux' package allows for multiplexing and demultiplexing of streams. It is a lightweight alternative to @metamask/object-multiplex and is suitable for simpler use cases where advanced features are not required.
@metamask/object-multiplex
Simple stream multiplexing for objectMode
.
Usage
const mux = new ObjMultiplex();
const streamA = mux.createStream("hello");
const streamB = mux.createStream("world");
mux.pipe(transport).pipe(mux);
streamA.write({ thisIsAn: "object" });
streamA.write(123);
streamB.pipe(evilAiBrain).pipe(streamB);
Contributing
Setup
- Install Node.js version 12
- If you are using nvm (recommended) running
nvm use
will automatically choose the right node version for you.
- Install Yarn v1
- Run
yarn setup
to install dependencies and run any requried post-install scripts
- Warning: Do not use the
yarn
/ yarn install
command directly. Use yarn setup
instead. The normal install command will skip required post-install scripts, leaving your development environment in an invalid state.
Testing and Linting
Run yarn test
to run the tests once.
Run yarn lint
to run the linter, or run yarn lint:fix
to run the linter and fix any automatically fixable issues.
Release & Publishing
The project follows the same release process as the other libraries in the MetaMask organization:
- Create a release branch
- For a typical release, this would be based on
main
- To update an older maintained major version, base the release branch on the major version branch (e.g.
1.x
)
- Update the changelog
- Update version in package.json file (e.g.
yarn version --minor --no-git-tag-version
) - Create a pull request targeting the base branch (e.g. master or 1.x)
- Code review and QA
- Once approved, the PR is squashed & merged
- The commit on the base branch is tagged
- The tag can be published as needed